package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.functions.T;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.TeacherMapper;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.pojo.RolePermission;
import org.hnxxxy.ebms.pojo.Teacher;
import org.hnxxxy.ebms.pojo.TeacherAdmiss;
import org.hnxxxy.ebms.mapper.TeacherAdmissMapper;
import org.hnxxxy.ebms.service.RolePermissionService;
import org.hnxxxy.ebms.service.RoleService;
import org.hnxxxy.ebms.service.TeacherAdmissService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.service.TeacherService;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.vo.TeacherAdmissVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@Service
public class TeacherAdmissServiceImpl extends ServiceImpl<TeacherAdmissMapper, TeacherAdmiss> implements TeacherAdmissService {

    @Autowired
    private TeacherAdmissMapper teacherAdmissMapper;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public Result<IPage<TeacherAdmissVo>> get(int page) {
        IPage<TeacherAdmissVo> iPage = new Page<>(page,10);
        IPage<TeacherAdmissVo> teacherAdmissVoIPage = teacherAdmissMapper.selectTeaAdmiss(iPage);
        return Result.success(teacherAdmissVoIPage);
    }

    @Override
    public Result<IPage<TeacherAdmissVo>> getName(int page, String name) {
        IPage<TeacherAdmissVo> iPage = new Page<>(page,10);
        IPage<TeacherAdmissVo> teacherAdmissVoIPage = teacherAdmissMapper.selectTeaAdmissByName(iPage,name);
        return Result.success(teacherAdmissVoIPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(TeacherAdmissVo admiss) {
        TeacherAdmiss teacherAdmiss = new TeacherAdmiss();
        teacherAdmiss.setAdmissionId(admiss.getAdmissionId());
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(admiss,teacher);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_name",teacher.getTeacherName())
                .eq("teacher_phone",teacher.getTeacherPhone());
        Teacher one = teacherService.getOne(queryWrapper);
        if (one == null){
            // 是否生成角色
            teacherMapper.insert(teacher);
            Role role = new Role();
            role.setRoleName(teacher.getTeacherName());
            role.setRoleUsername(teacher.getTeacherPhone());
            String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            // 获取salt
            Random random = new Random();
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 16; i++) {
                int s = random.nextInt(62);
                stringBuilder.append(str.charAt(s));
            }
            String salt = stringBuilder.toString();
            // 密码加密
            role.setRolePassword(CommonUntil.bCrypt(teacher.getTeacherPhone(),salt));
            role.setRoleSalt(salt);
            role.setIsTeacher(false);
            // 搜索刚刚插入的老师信息
            queryWrapper.eq("teacher_name",teacher.getTeacherName())
                    .eq("teacher_phone",teacher.getTeacherPhone());
            Teacher serviceOne = teacherService.getOne(queryWrapper);
            role.setUserId(serviceOne.getTeacherId());
            RolePermission rolePermission = new RolePermission();
            roleService.save(role);
            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq("role_name",teacher.getTeacherName())
                    .eq("role_username",teacher.getTeacherPhone());
            Role one1 = roleService.getOne(roleQueryWrapper);
            rolePermission.setRoleId(one1.getRoleId());
            rolePermission.setPermissionId(5);
            teacherAdmiss.setTeacherId(serviceOne.getTeacherId());
            rolePermissionService.save(rolePermission);
        }else {
            teacherAdmiss.setTeacherId(admiss.getTeacherId());
        }
        this.save(teacherAdmiss);
        return Result.success(200,"添加成功");
    }

    @Override
    public Result<String> updateTeaAdmiss(TeacherAdmissVo admiss) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(admiss,teacher);
        teacherService.updateById(teacher);
        TeacherAdmiss teacherAdmiss = new TeacherAdmiss();
        teacherAdmiss.setTeacherId(admiss.getTeacherId());
        teacherAdmiss.setAdmissionId(admiss.getAdmissionId());
        teacherAdmiss.setTeacherAdmissId(admiss.getTeacherAdmissId());
        this.baseMapper.updateById(teacherAdmiss);
        return Result.success(200,"修改成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> del(Integer id) {
        // 删除地区信息
        if (this.removeById(id)){
            // 获取角色信息 删除角色 和老师信息
            Role role = roleService.getById(CommonUntil.getId());
            if (roleService.removeById(role.getRoleId()) && teacherService.removeById(role.getUserId())){
                return Result.success(200,"删除成功");
            }else {
                return Result.fail("删除失败");
            }
        }
        return Result.fail("删除失败");
    }
}
